package com.ruoyi.web.controller.leftover;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.config.RuoYiConfig;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.Company;
import com.ruoyi.common.core.domain.entity.Leftover;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.file.FileUploadUtils;
import com.ruoyi.system.service.ICompanyService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.tomcat.jni.FileInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;

import static com.ruoyi.common.utils.PageUtils.startPage;

/**
 * 边角料控制层
 *
 * @author C3007106
 */
@Controller
@RequestMapping("/system/company")
public class CompanyController extends BaseController {

    private String prefix = "leftover/company";

    @Autowired
    private ICompanyService companyService;

    @GetMapping("/view")
    public String view() {
        return prefix + "/view";
    }


    /**
     * 新增公司
     */
    @GetMapping("/add")
    public String add() {
        return prefix + "/add";
    }

    /**
     * 新增保存公司
     */
    @RequiresPermissions("system:company:add")
    @Log(title = "公司管理", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(@RequestParam("file") MultipartFile file, FileInfo fileInfo, Company company) throws IOException {
        // 上传文件路径
        String filePath = RuoYiConfig.getUploadPath();
        // 上传并返回新文件名称
        String fileName = FileUploadUtils.upload(filePath, file);
//        fileInfo.setFilePath(fileName);
        company.setReceiptContract(fileName);
        companyService.insertCompany(company);
        return AjaxResult.success();
    }


    @RequiresPermissions("system:company:remove")
    @Log(title = "公司管理", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        return toAjax(companyService.deleteCompanyById(ids));
    }

    @RequiresPermissions("system:company:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(Company company) {
        startPage();
        List<Company> list = companyService.selectCompanyList(company);
        for (Company company1 : list) {
            company1.setNum(company1.getReceiptNum() - company1.getReceiptNowNum());
        }
        return getDataTable(list);
    }

    /**
     * 修改
     */
    @RequiresPermissions("system:company:edit")
    @GetMapping("/edit/{tableId}")
    public String edit(@PathVariable("tableId") String tableId, ModelMap mmap) {
        mmap.put("company", companyService.selectCompanyByTableId(tableId));
        return "leftover/company/edit";
    }

    /**
     * 修改保存
     */
    @RequiresPermissions("system:company:edit")
    @Log(title = "公司信息管理", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(@Validated Company company) {
        return toAjax(companyService.updateCompany(company));
    }
}
